package com.masterclient.app.businessmodule.minemodule.cash.detail

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.eoner.baselib.widget.navgation.FCNavigationBar
import com.masterclient.app.R
import com.masterclient.app.businessmodule.minemodule.cash.bean.CashDetailBean
import com.masterclient.app.businessmodule.minemodule.cash.bean.CashInfoBean
import com.masterclient.app.businessmodule.minemodule.cash.contract.CashInfoContract
import com.masterclient.app.businessmodule.minemodule.cash.contract.CashInfoPresenter
import com.eoner.baselibrary.utils.GlideUtil
import com.eoner.baselibrary.utils.Utils
import com.masterclient.app.base.FCBusinessActivity
import com.masterclient.app.widget.PriceIntegralLayout
import kotlinx.android.synthetic.main.activity_cash_detail.*
import kotlinx.android.synthetic.main.item_cash_detail.view.*
import kotlinx.android.synthetic.main.item_customer_order_shop.view.*

/**
 * 作者：sean
 * 创建时间：2018/5/14 15:25
 * 功能说明：
 */
class GainInfoActivity : FCBusinessActivity<CashInfoPresenter>(),CashInfoContract.View{


    override fun requireGetLayoutId(): Int {
        return R.layout.activity_cash_detail;
    }

    override fun requireSetCustomNavigationBar(navigationBar: FCNavigationBar?) {
        if("1".equals(getBundleString("type"))){
            navigationBar?.setTitle("获取详情")
        } else {
            navigationBar?.setTitle("支出详情")
        }
    }

    override fun requireInitUIAndData(savedInstanceState: Bundle?) {
        Utils.setDin(tv,mContext)
        mPresenter.getBalanceDetail(getBundleString("id"))
    }

    override fun showData(dataBean: CashInfoBean.DataBean) {

    }

    @SuppressLint("SetTextI18n")
    override fun showCashDetailData(dataBean: CashDetailBean.DataBean) {
//        if(dataBean.sh_amount.toFloat()>0){
//            tv.text = "+"+dataBean.sh_amount
//        }else {
//            if (dataBean.sh_amount.toFloat() == 0.0f){
//                tv.text = dataBean.sh_amount
//            }else{
//                tv.text = "-"+dataBean.sh_amount
//            }
//        }
        if (dataBean.sh_amount.toFloat() == 0.0f){
            tv.text = dataBean.sh_amount
        }else{
            if("2".equals(getBundleString("type"))){
                tv.text = "-"+dataBean.sh_amount
            }else{
                tv.text = "+"+dataBean.sh_amount
            }
        }
        if(dataBean.sh_tip!=null&&dataBean.sh_tip.size>0){
            rl_con.visibility =View.VISIBLE
            for(itemBean in dataBean.sh_tip){
                var item = View.inflate(mContext,R.layout.item_cash_detail,null);
                item.tv_tag.text = itemBean.sh_title
                item.tv_desc.text = itemBean.sh_content
                ll_con.addView(item)
            }
        }

        if(dataBean.sh_order_product!=null&&dataBean.sh_order_product.size>0){
            ll_return.visibility = View.VISIBLE
            for (i in 0 until dataBean.sh_order_product.size) {
                val productsBean = dataBean.sh_order_product[i]
                val view_goods = View.inflate(mContext, R.layout.item_customer_order_shop_goods, null)
                val v_divide = view_goods.findViewById<View>(R.id.v_divide)
                val imageView = view_goods.findViewById<View>(R.id.iv_good) as ImageView
                val tv_title = view_goods.findViewById<View>(R.id.tv_title) as TextView
                val tv_sku = view_goods.findViewById<View>(R.id.tv_skus) as TextView
                val ll_price = view_goods.findViewById<View>(R.id.ll_price) as PriceIntegralLayout

                val tv_gos_num = view_goods.findViewById<View>(R.id.tv_gos_num) as TextView
                val tv_after_sale = view_goods.findViewById<View>(R.id.tv_after_sale) as TextView
                GlideUtil.displayRoundImage(mContext, productsBean.sh_image, imageView, 4, R.mipmap.defaults, 107, 107)
                //                    App.picasso.load(productsBean.getSh_image())
                //                            .transform(new NewPicassoRoundTransform(ScreenUtils.dp2px(4)))
                //                            .resize(ScreenUtils.dp2px(107),ScreenUtils.dp2px(107))
                //                            .error(R.mipmap.defaults).placeholder(R.mipmap.defaults).into(imageView);
                tv_title.text = productsBean.sh_name
                val view_line = view_goods.findViewById<View>(R.id.view_line)
                v_divide.visibility = if (i == 0) View.GONE else View.VISIBLE
                view_line.visibility = if (i == dataBean.sh_order_product.size - 1) View.GONE else View.VISIBLE
                tv_after_sale.visibility = View.GONE
                var skus = ""
                val sh_property = productsBean.sh_property
                for (propertyBean in sh_property) {
                    skus += "“" + propertyBean.sh_alias_name + "”"
                }
                tv_sku.text = "规格：$skus"
                ll_price.setData(productsBean.sh_integral,productsBean.sh_price)
//                if (productsBean.sh_integral != null && java.lang.Float.valueOf(productsBean.sh_integral) > 0) {
//                    val temp = (java.lang.Float.valueOf(productsBean.sh_integral)!! / java.lang.Float.valueOf(productsBean.sh_buy_qty)!!).toInt()
//                    val content = "$temp 金币"
//                    val spannableString = SpannableString(content)
//                    spannableString.setSpan(RelativeSizeSpan(0.75f), content.length - 3, content.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
//                    tv_price.text = spannableString
//                    //                        tv_price.setText(temp + "金币");
//                } else {
//                    Utils.set_price_style(tv_price, "¥" + productsBean.sh_price)
//                }
                tv_gos_num.text = "x" + productsBean.sh_buy_qty
                ll_return_container.addView(view_goods)
            }
        }

        if(dataBean.sh_orders!=null&&dataBean.sh_orders.size>0){
            ll_orders.visibility = View.VISIBLE
            for (ordersBean in dataBean.sh_orders) {
                val view = View.inflate(mContext, R.layout.item_customer_order_shop, null)
                view.ll_layout.visibility = View.VISIBLE
                view.line.visibility = View.GONE

                view.findViewById<View>(R.id.order_no_ll).visibility = View.VISIBLE
                view.order_no_tv.text = ordersBean.sh_order_noX

                view.tv_shop_name.text = ordersBean.getSh_seller().sh_store_nameX
                val ll_shop_container = view.findViewById<View>(R.id.ll_shop_container) as LinearLayout
//                ll_shop_container.setOnClickListener(v->{
//                    Intent intent = new Intent(mContext, OrderDetailActivity.class);
//                    intent.putExtra("order_no",ordersBean.getSh_order_no());
//                    mContext.startActivity(intent);
//                });
                ll_shop_container.removeAllViews()
                var i = 0
                for (productsBean in ordersBean.getSh_products()) {
                    val view_goods = View.inflate(mContext, R.layout.item_customer_order_shop_goods, null)
                    val v_divide = view_goods.findViewById<View>(R.id.v_divide)
                    val imageView = view_goods.findViewById<View>(R.id.iv_good) as ImageView
                    val tv_title = view_goods.findViewById<View>(R.id.tv_title) as TextView
                    val tv_sku = view_goods.findViewById<View>(R.id.tv_skus) as TextView
                    val ll_price = view_goods.findViewById<View>(R.id.ll_price) as PriceIntegralLayout
//                    Utils.setDin(tv_price, mContext)
                    val tv_gos_num = view_goods.findViewById<View>(R.id.tv_gos_num) as TextView
                    val tv_after_sale = view_goods.findViewById<View>(R.id.tv_after_sale) as TextView
                    GlideUtil.displayRoundImage(mContext, productsBean.getSh_image(), imageView, 4, R.mipmap.defaults, 107, 107)
                    tv_title.setText(productsBean.getSh_name())
                    val view_line = view_goods.findViewById<View>(R.id.view_line)

                    view_line.visibility = if (i == ordersBean.getSh_products().size - 1) View.GONE else View.VISIBLE
                    if (i == 0) {
                        v_divide.visibility = View.GONE
                    } else {
                        v_divide.visibility = View.VISIBLE
                    }

                    i++

                    tv_after_sale.visibility = View.GONE

                    var skus = ""
                    val sh_property = productsBean.getSh_property()
                    for (propertyBean in sh_property) {
                        skus += "“" + propertyBean.getSh_alias_name() + "”"
                    }
                    tv_sku.text = "规格：$skus"

                    ll_price.setData(((java.lang.Float.valueOf(productsBean.sh_integral)!! / java.lang.Float.valueOf(productsBean.sh_buy_qty)!!).toInt()).toString(), productsBean.sh_price)

//                    if (productsBean.sh_integral != null && java.lang.Float.valueOf(productsBean.sh_integral) > 0) {
//                        val temp = (java.lang.Float.valueOf(productsBean.sh_integral)!! / java.lang.Float.valueOf(productsBean.sh_buy_qty)!!).toInt()
//                        val content = "$temp 金币"
//                        val spannableString = SpannableString(content)
//                        spannableString.setSpan(RelativeSizeSpan(0.75f), content.length - 3, content.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
//                        tv_price.text = spannableString
//                        //                        tv_price.setText(temp + "金币");
//                    } else {
//                        Utils.set_price_style(tv_price, "¥" + productsBean.sh_price)
//                    }

                    tv_gos_num.text = "x" + productsBean.getSh_buy_qty()
                    ll_shop_container.addView(view_goods)
                }
                ll_prices.visibility = View.VISIBLE
                tv_all_price.text = "¥" + dataBean.sh_procuct_price
                tv_freight.text = "¥" + dataBean.sh_shipping_total
                tv_discount.text = "¥" + dataBean.sh_discount_total
                tv_cash_count.text = "¥" + dataBean.sh_balance_total
                tv_final_price.text = "¥" + dataBean.sh_subtotal
                ll_orders.addView(view)
            }
        }
    }

    override fun onClickPageStateErrorView() {
        super.onClickPageStateErrorView()
        mPresenter.getBalanceDetail(getBundleString("id"))
    }

}